import 'package:my_flutter_core/config/general_config.dart';

/// Gets current client date time.
DateTime getCurrentDateTime() {
  return DateTime.now();
}

/// Constructs a new [DateTime] instance based on [formattedString].
/// If [GeneralConfig.enableUtcFormat] == true it parses UTC format string to date and returns in local time zone.
DateTime parseDate(String formattedString) {
  final date = DateTime.parse(formattedString);

  return GeneralConfig.enableUtcFormat ? date.toLocal() : date;
}

/// Gets [DateTime] instance based on time string.
/// [time] format is like '09:00'.
DateTime getFullDateTime(String time) {
  final now = getCurrentDateTime();
  final timeParts = time.split(':');

  return DateTime(
    now.year,
    now.month,
    now.day,
    int.parse(timeParts[0]),
    int.parse(timeParts[1]),
  );
}

/// Gets the first day based on [year] and [month].
///
/// If [month] is null then the default month will be Jan.
DateTime getFirstDay(int year, int? month) {
  return DateTime(year, month ?? 1, 1);
}

/// Gets the last day based on [year] and [month].
///
/// If [month] is null then the default month will be Jan.
DateTime getLastDay(int year, int month) {
  final firstDayOfNextMonth = getFirstDay(year, (month) + 1);
  return firstDayOfNextMonth.subtract(const Duration(days: 1));
}
